#include "widget.h"
#include "./ui_widget.h"
#include "store.h"
#include "trade.h"
#include "reqsn.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    // 窗口属性|Qt::WindowType::BypassWindowManagerHint|Qt::WindowStaysOnTopHint
    setAttribute(Qt::WA_TranslucentBackground);
    setWindowFlags(Qt::WindowType::FramelessWindowHint);
    // resize(QSize(1920,1080));

    SysIcon = new QLabel;
    SysIcon->setPixmap(QPixmap(":images/title2.png").scaled(24,24));
    SysTitle = new QLabel;
    SysTitle->setText("商户营业端");
    SysTitle->setStyleSheet("color:white;");
    minWindow = new QAction(style()->standardIcon(QStyle::SP_TitleBarMinButton),"最小化");
    maxWindow = new QAction(style()->standardIcon(QStyle::SP_TitleBarMaxButton),"最大化");
    endWindow = new QAction(style()->standardIcon(QStyle::SP_TitleBarCloseButton),"关闭");
    // minWindow->setIcon(setIconColor(minWindow->icon(),QColor("white")));
    // maxWindow->setIcon(setIconColor(maxWindow->icon(),QColor("white")));
    // endWindow->setIcon(setIconColor(endWindow->icon(),QColor(255,255,255,255)));
    SysToolBar = new QToolBar;
    SysToolBar->setIconSize(QSize(16,16));
    SysToolBar->addAction(minWindow);
    SysToolBar->addAction(maxWindow);
    SysToolBar->addAction(endWindow);
    connect(SysToolBar,&QToolBar::actionTriggered, this, &Widget::titleBarEvent);

    QHBoxLayout *topLayout = new QHBoxLayout;
    topLayout->setContentsMargins(0,0,0,0);
    topLayout->addWidget(SysIcon);
    topLayout->addWidget(SysTitle);
    topLayout->addStretch(1);
    topLayout->addWidget(SysToolBar);

    Trade *tradeWidget = new Trade;
    QVBoxLayout *tradeLayout = new QVBoxLayout;
    tradeLayout->setContentsMargins(0,0,0,0);
    tradeLayout->addWidget(tradeWidget);
    tradeLayout->addStretch();

    storeWidget = new Store;
    QVBoxLayout *storeLayout = new QVBoxLayout;
    storeLayout->setContentsMargins(0,0,0,0);
    storeLayout->addWidget(storeWidget);
    storeLayout->addStretch();

    Reqsn *reqsnWidget = new Reqsn;
    QVBoxLayout *reqsnLayout = new QVBoxLayout;
    reqsnLayout->setContentsMargins(0,0,0,0);
    reqsnLayout->addWidget(reqsnWidget);
    reqsnLayout->addStretch();

    QHBoxLayout *bodyLayout = new QHBoxLayout;
    bodyLayout->setContentsMargins(0,0,0,0);
    bodyLayout->addLayout(tradeLayout);
    bodyLayout->addLayout(storeLayout);
    bodyLayout->addLayout(reqsnLayout);

    // 自定义组件
    // quitButton = new QPushButton(tr("&Quit"));
    // connect(quitButton, &QPushButton::clicked,
    //         qApp, &QCoreApplication::quit);
    // QHBoxLayout *bottomLayout = new QHBoxLayout;
    // bottomLayout->addStretch();
    // bottomLayout->addWidget(quitButton);

    QVBoxLayout *mainLayout = new QVBoxLayout;
    mainLayout->setContentsMargins(5,5,5,5);
    mainLayout->addLayout(topLayout);
    mainLayout->addLayout(bodyLayout);
    // mainLayout->addStretch(1);
    setLayout(mainLayout);

    setStyleSheet("Widget {background-color: qlineargradient(spread:pad, x1:1, y1:0, x2:0, y2:1, stop:0 rgba(14,70,200, 255), stop:1 rgba(255, 255, 255, 255)); border-radius: 5px;}");

    // qDebug() << "token" << www->token;

}

Widget::~Widget()
{
    delete ui;
}

void Widget::resizeEvent(QResizeEvent *event)
{
    event->accept();   
    storeWidget->reSizeMainContainer(Widget::size().height());    
}
void Widget::paintEvent(QPaintEvent *event){
    QStyleOption opt;
    opt.initFrom(this);
    QPainter p(this);
    style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
}

void Widget::mousePressEvent(QMouseEvent *event){
    // std::cout << event->button() << std::endl;
    if(event->button() == Qt::LeftButton){
        if(isMaximized() || isFullScreen()){return;}
        QPoint pos = event->globalPosition().toPoint();
        this->m_flag = true;
        this->m_Position = pos - this->pos();
        event->accept();
        setCursor(QCursor(Qt::OpenHandCursor));
    }
}

void Widget::mouseMoveEvent(QMouseEvent *event){
    if((event->buttons() == Qt::LeftButton) && (this->m_flag)){
        // if(isMaximized() | isFullScreen()){this->showNormal();}
        // std::cout << "test_01" << std::endl;
        QPoint pos = event->globalPosition().toPoint();
        this->move(pos-this->m_Position);
        event->accept();
    }
}

void Widget::mouseReleaseEvent(QMouseEvent *event){
    this->m_flag = false;
    setCursor(QCursor(Qt::ArrowCursor));
}

void Widget::titleBarEvent(QAction *event){
    // std::cout << "test_01" << std::endl;
    if(event->text() == "最小化"){
        showMinimized();
    }else if(event->text() == "最大化"){
        maxWindow->setText("默认");
        maxWindow->setIcon(style()->standardIcon(QStyle::SP_TitleBarNormalButton));
        showFullScreen();
        // QSize size = Widget::size();
        // storeWidget->reSizeMainContainer(size.height());
    }else if(event->text() == "默认"){
        // storeWidget->reSizeMainContainer(600);
        maxWindow->setText("最大化");
        maxWindow->setIcon(style()->standardIcon(QStyle::SP_TitleBarMaxButton));        
        showNormal();      
    }else if(event->text() == "关闭"){
        close();
    }

    qDebug() << "测试全局变量:" << www->token;
}

void Widget::reSizeMainContainer()
{
    // QPoint pos = Widget::pos(); // x(),y()
    // std::cout << "---pos---" << std::endl;
    // std::cout << pos.x() << std::endl;
    // std::cout << pos.y() << std::endl;

    // QRect rect = Widget::rect();// left(),top(),right(),bottom(),height(),width()
    // std::cout << "---rect---" << std::endl;
    // std::cout << rect.width() << std::endl;
    // std::cout << rect.height() << std::endl;
    // std::cout << rect.left() << std::endl;
    // std::cout << rect.top() << std::endl;
    // std::cout << rect.right() << std::endl;
    // std::cout << rect.bottom() << std::endl;

    QSize size = Widget::size(); // width(),height()
    // std::cout << "---main size---" << std::endl;
    // std::cout << size.width() << std::endl;
    std::cout << size.height() << std::endl;

    storeWidget->reSizeMainContainer(size.height());
}

QIcon Widget::setIconColor(QIcon icon, QColor color)
{
    // 仅作用于button类
    QPixmap pixmap = icon.pixmap(QSize(64,64));
    QPainter painter(&pixmap);
    painter.setCompositionMode(QPainter::CompositionMode_SourceIn);
    painter.fillRect(pixmap.rect(), color);
    icon.addPixmap(pixmap);
    return icon;
}
